﻿using System;
using System.Web;

namespace BestEasyCMS.Framework.CookieStorage
{
    public class CookieStorageService : ICookieStorageService
    {
        #region 添加Cookie方法

        /// <summary>
        /// 添加Cookie，默认不能跨域访问
        /// </summary>
        /// <param name="cookie">Http Cookie对象</param>
        public void AddCookie(HttpCookie cookie)
        {
            AddCookie(cookie, null);
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookie">Http Cookie对象</param>
        /// <param name="domain">Cookie的作用域，如需要跨域访问则填写，如：****.一级域名.com</param>
        public void AddCookie(HttpCookie cookie, string domain)
        {
            HttpResponse response = HttpContext.Current.Response;
            //指定客户端脚本是否可以访问[默认为false]
            cookie.HttpOnly = true;
            //指定统一的Path，比便能通存通取
            cookie.Path = "/";
            if (domain != null)
            {
                //设置跨域,这样在其它二级域名下就都可以访问到了
                cookie.Domain = domain;
            }
            response.AppendCookie(cookie);
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="key">Cookie对象的存在的Key</param>
        /// <param name="value">Cookie子键的值</param>
        public void AddCookie(string key, string value)
        {
            if (key == null || value == null)
                return;

            AddCookie(new HttpCookie(key, value));
        }

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="key">Cookie对象的存在的Key</param>
        /// <param name="value">Cookie子键的值</param>
        /// <param name="expires">过期时间</param>
        public void AddCookie(string key, string value, DateTime expires)
        {
            if (key == null || value == null)
                return;

            var cookie = new HttpCookie(key, value) { Expires = expires };
            AddCookie(cookie);
        }

        /// <summary>
        /// 添加为Cookie.Values集合
        /// </summary>
        /// <param name="cookieName">Cookie名称</param>
        /// <param name="key">Cookie对象的存在的Key</param>
        /// <param name="value">Cookie子键的值</param>
        public void AddCookie(string cookieName, string key, string value)
        {
            if (cookieName == null || key == null || value == null)
                return;

            var cookie = new HttpCookie(cookieName);
            cookie.Values.Add(key, value);
            AddCookie(cookie);
        }

        /// <summary>
        /// 添加为Cookie.Values集合
        /// </summary>
        /// <param name="cookieName">Cookie名称</param>
        /// <param name="key">Cookie对象的存在的Key</param>
        /// <param name="value">Cookie子键的值</param>
        /// <param name="expires">过期时间</param>
        public void AddCookie(string cookieName, string key, string value, DateTime expires)
        {
            if (cookieName == null || key == null || value == null)
                return;

            var cookie = new HttpCookie(cookieName) { Expires = expires };
            cookie.Values.Add(key, value);
            AddCookie(cookie);
        }

        #endregion 添加Cookie方法

        #region 获取Cookie的方法

        /// <summary>
        /// 获得Http Cookie
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <returns>Cookie</returns>
        public HttpCookie GetCookie(string cookieName)
        {
            HttpRequest request = HttpContext.Current.Request;
            return request.Cookies[cookieName];
        }

        /// <summary>
        /// 获得Cookie的值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="key">cookie名称中的Key</param>
        /// <returns>Cookie值</returns>
        public string GetCookieValue(string cookieName, string key)
        {
            HttpRequest request = HttpContext.Current.Request;
            return GetCookieValue(request.Cookies[cookieName], key);
        }

        /// <summary>
        /// 获得Cookie的子键值
        /// </summary>
        /// <param name="cookie">Http Cookie对象</param>
        /// <param name="key">Cookie对象的存在的Key</param>
        /// <returns>Cookie的子键值</returns>
        public string GetCookieValue(HttpCookie cookie, string key)
        {
            if (cookie != null)
            {
                if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                    return cookie.Values[key];
                return cookie.Value;
            }
            return "";
        }

        #endregion 获取Cookie的方法

        #region 设置Cookie方法

        /// <summary>
        /// 设置Cookie中指定键的值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="key">Cookie对象的存在的Key</param>
        /// <param name="value">Cookie子键的值</param>
        public void SetCookie(string cookieName, string key, string value)
        {
            SetCookie(cookieName, key, value, null);
        }

        /// <summary>
        /// 设置整个Cookie过期时间
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="expires">过期时间</param>
        public void SetCookie(string cookieName, DateTime expires)
        {
            SetCookie(cookieName, null, null, expires);
        }

        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="key">Cookie对象的存在的Key</param>
        /// <param name="value">Cookie子键的值</param>
        /// <param name="expires">过期时间</param>
        public void SetCookie(string cookieName, string key, string value, DateTime? expires)
        {
            if (cookieName == null) return;

            HttpResponse response = HttpContext.Current.Response;
            HttpCookie cookie = response.Cookies[cookieName];

            if (cookie == null) return;

            if (!String.IsNullOrEmpty(key) && !String.IsNullOrEmpty(value))
            {
                cookie.Values.Set(key, value);
            }

            if (expires != null)
            {
                cookie.Expires = expires.Value;
            }

            response.SetCookie(cookie);
        }

        #endregion 设置Cookie方法

        #region 移除Cookie方法

        /// <summary>
        /// 删除指定的Cookie
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        public void RemoveCookie(string cookieName)
        {
            RemoveCookie(cookieName, null);
        }

        /// <summary>
        /// 删除Cookie中指定的键
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="key">Cookie对象的存在的Key</param>
        public void RemoveCookie(string cookieName, string key)
        {
            HttpResponse response = HttpContext.Current.Response;
            HttpCookie cookie = response.Cookies[cookieName];
            if (cookie != null)
            {
                if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                    cookie.Values.Remove(key);
                else
                {
                    HttpCookie httpCookie = response.Cookies[cookieName];
                    if (httpCookie != null)
                        httpCookie.Expires = DateTime.Now.AddYears(-1);
                }
            }
        }

        #endregion 移除Cookie方法

    }

}
